<?php defined('IN_LK') or die('Access Denied');
lk::lo_app('index_common', 'index', 0);
/**
 * 评论处理
 */
class comment extends index_common
{
	private $db  = null;
	private $mdb = null;
	
	public function __construct()
	{
		parent::__construct();
		$this->db  = lk::load_model('comment');
		$this->mdb = lk::load_model('member');
	}
	
	public function initialize()
	{
		
	}
	
	public function list_comment()
	{
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		$id   = isset($_GET['id'])  ? (int)$_GET['id']  : exit('');
		$data = $this->db->listinfo('*', "`cid`='$id' AND `disable`='0'", $page, 20, '`id` DESC');
		include template('comment_list');
	}
	
	/**
	 * ajax评论
	 */
	public function feedback_ajax()
	{
		$msg = array('msg'=>0, 'data'=>'');
		if(!isset($_POST['cid']) || !$_POST['cid'] || 
		   !isset($_POST['msg']) || !$_POST['msg'] || strlen($_POST['msg'])>500 || 
		   !isset($_POST['star']))
		{
			exit(json_encode($msg));
		}
		if(isset($_POST['username']) && $_POST['username'] && strtolower($_SESSION['captcha']) != strtolower($_POST['captcha']))
		{
			exit(json_encode($msg));
		}
		if(isset($_POST['username']) && isset($_POST['password']) && $_POST['username'] && $_POST['password'])
		{
			$pwd = $this->mdb->login_check($_POST['username'], $_POST['password'], 1);
			$this->mdb->dologin($_POST['username'], $pwd, 0, 1);
		}
		if(isset($_SESSION['_username']) && $_SESSION['_username'])
		{
			$info['username'] = isset($_POST['notuser']) && $_POST['notuser'] ? '匿名' : $_SESSION['_username'];
		}
		else
		{
			exit(json_encode($msg));
		}
		$content = lk::load_model('content');
		$info['cid']  = (int)$_POST['cid'];  //检查文章是否存在且允许评论
		$info['ctitle']= $content->id2title($info['cid']);
		if(!$info['ctitle'])
		{
			exit(json_encode($msg));
		}
		if(!$this->db->comment_check($info['cid']))
		{
			$msg['msg'] = 2;
			exit(json_encode($msg));
		}
		$info['star']   = $_POST['star']> 5 ? 5 : max((int)$_POST['star'], 0);
		$info['msg']    = $_POST['msg'];
		$info['ip']     = get_ip();
		$info['dtime']  = TIME;
		$info['userid'] = $_SESSION['_userid'];
		$r = $this->db->insert($info);
		if($r)
		{
			$f = $this->db->feedback($info['cid']);  //好评度
			$content->update(array('feedback'=>$f), "`id`='" . $info['cid'] . "'");
			$msg['msg'] = 1;
			$msg['data'] = "<dd><script type=\"text/javascript\">
$(\"#star{$r}\").raty({'path':'" . IMG_PATH . "jiajiao/raty/','hintList':['踩', '不咋地', '一般', '很好', '好极了'],'readOnly':true,'start':" . $info['star'] . "});</script><div class=\"star\" id=\"star{$r}\"></div><div class=\"starf\">评价：</div><div class=\"b_star\">" . $info['username'] . "&nbsp;<span class=\"aaa\">" . date('m-d H:i', TIME) . "</span>&nbsp;说：</div><div class=\"co\">" . $info['msg'] . "</div></dd>";
			exit(json_encode($msg));
		}
		exit(json_encode($msg));
	}
}